import requests
import json
from dbUtil import db
headers = {
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
    ,"referer":"https://www.zhipin.com/web/geek/job?query=%E8%BF%90%E7%BB%B4&city=101240100"
    ,"cookie":"ab_guid=51235c12-cfb1-4004-b2f9-bea9c0aaba5b; __g=-; Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1748166523; HMACCOUNT=2C8578AFEE343F47; lastCity=101240200; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1748166834; __zp_stoken__=15d6fPTTDnMK4wpPCvjkvCxAPCgs7IzI0KCw8PShAP0c9ND49RT00Rh8zLcKowr7Dt8ObBFvDgcKSwrM0IT4%2FOz00QD80QBE%2BO8SywrI9PxPCuMK9w6zDmQ5bw4IzPwTDvcKyD8OYwrheRcK9DWLCtCguwobCvjMyOzFYwr4ywrkBwr0zw4ADwrQ4wrkyMzE4KT4JCw4OPjNCQV4IQWZIXFpVA0lKSyExOz09w7fDrsO4Nz8JCwoJEAMBCAMCDw0EDg8NDw4NBA8NBA8OLD7CoMKzw6rElsanwq7EmcWow4rCtsSTw6LDpcKNw5XDosOYwp%2FFhsO%2Fw5TCvMOuwrnEocKqxLHDh8SFwqXEucK1w67Cl8OEwqvDtcKew5DDgMOcwqTDg8KRwprCm8KPwrnCtcK3wpDCrsKewr%2FCoU7DukrCusK9wrZTwqBBw73CsMKpUMKpRMKDcE%2FCssKiWcK%2BUsKjZsKvwrNCTmfDhkpUwrlUwr1Ne29bd10MZRANP15YwrA%3D; __c=1748166521; __l=l=%2Fwww.zhipin.com%2Fweb%2Fgeek%2Fjobs%3Fquery%3D%25E8%25BF%2590%25E7%25BB%25B4%26city%3D101240100&r=&g=&s=3&friend_source=0&s=3&friend_source=0; __a=62861351.1748166521..1748166521.7.1.7.7"}
job = input("请输入查询的工作:")
citya = input("请输入查询的地点:")
cityDic = {"全国":"100010000","南昌":"101240100","北京":"101010100",
           "上海":"101020100","广州":"101280100","深圳":"101280600",
"杭州":"101210100","天津":"101030100","西安":"101110100",
"苏州":"101190400","武汉":"101200100","厦门":"101230200",
"长沙":"101250100","成都":"101270100","郑州":"101180100"
           }
city = cityDic.get(citya,"100010000")
url = "https://www.zhipin.com/wapi/zpgeek/search/joblist.json?scene=1&query="+job+"&city="+city+"&experience=&payType=&partTime=&degree=&industry=&scale=&stage=&position=&jobType=&salary=&multiBusinessDistrict=&multiSubway=&page=1&pageSize=100&_=1741750103962"
resp = requests.get(url, headers=headers)
text = json.loads(resp.content.decode())
print(text)
# 获取数据
data = text["zpData"]["jobList"]
# 获取操作数据的对象
cursor = db.cursor()
for i in data:
    sql = '''
        insert into s_job values
        (null,'%s','%s','%s','%s','%s','%s')
    '''%(i["cityName"],i["brandName"],i["jobName"],i["jobDegree"],
         i["jobExperience"],i["salaryDesc"])
    try:
        cursor.execute(sql) # 执行sql
        db.commit() #提交数据
    except:
        print("数据有问题")
        db.rollback()